import math
from typing import List


from algs.common import OilWellData
from passcheck.pas_common import PassParams, PassCheckResult, InterCheckResult, BasePassParams, get_test_params, \
    params_init, data_init


# 计算几何通过性
def geometric_passability(params: PassParams, _list_: List[OilWellData]) -> float:
    l_max: float = 0.0

    for item in _list_:
        # factor_a: float = item.curvature_radius
        factor_a: float = 11551.568
        factor_b: float = factor_a + 0.5 * params.d_max1 + 0.5 * params.d_max2

        if 0 < item.depth <= params.lc1:
            l_max = 2 * math.sqrt(math.pow(factor_a + params.dc1, 2) - math.pow(factor_b, 2))
        # elif params.lc1 < item.depth < params.lcn:
        #     l_max = 2 * math.sqrt(math.pow(factor_a + params.dcn, 2) - math.pow(factor_b, 2))
        # elif params.lcn < item.depth < params.loh:
        #     l_max = 2 * math.sqrt(math.pow(factor_a + params.doh, 2) - math.pow(factor_b, 2))
    print("l_max:", l_max)
    # 如果得到最长度L_MAX >= L1 那么就输出通过，用1代替，如果得到最长度L_MAX < L1 那么就输出不通过，用2代替
    if l_max >= params.L1:
        print(1)
        return 1
    elif l_max < params.L1:
        print(2)
        return 2


# 井下工具所受的液体阻力
def liquid_resistance(params: PassParams, _list_: List[OilWellData], d:float) -> float:
    rate1: float = params.dc1 / params.d_max1
    # mn: float = params.dcn / params.d_max1
    # moh: float = params.doh / params.d_max1
    factor_a: float = 2 * math.pi * params.tao_p * params.L1 * params.v
    for item in _list_:
        if (item.depth > 0) and (item.depth <= params.lc1):
            factor_b: float = math.pow(rate1, 2) - 1
            factor_c: float = math.pow(rate1, 2) + 1
            fs = factor_a * (factor_b / (factor_c * math.log(rate1) - factor_b))
            print("fs:", fs)
            return fs
        # elif (item.depth > params.lc1) and (item.depth < params.lcn):
        #     factor_b: float = math.pow(mn, 2) - 1
        #     factor_c: float = math.pow(mn, 2) + 1
        #     fs = factor_a * (factor_b / (factor_c * math.log(mn) - factor_b))
        # elif (item.depth > params.lcn) and (item.depth < params.loh):
        #     factor_b: float = math.pow(moh, 2) - 1
        #     factor_c: float = math.pow(moh, 2) + 1
        #     fs = factor_a * (factor_b / (factor_c * math.log(moh) - factor_b))
    # return fs


# 井下工具所叠加的液体阻力
def all_liquid_resistance(params: PassParams, _list_: List[OilWellData], d: float) -> float:
    for item in _list_:
        if item.depth < params.L1:
            fs_all: float = 0
            return fs_all
        elif (item.depth >= params.L1) and (item.depth <= params.lc1):
            fs_all = all_liquid_resistance(params,_list_, item.depth-1) + liquid_resistance(params, _list_, item.depth)
            print("fs_all:", fs_all)
            return fs_all


# 井下工具所受的的摩阻力和轴向力
from model.soft.soft_common import UniForceResult


def frictional_and_axial(params:PassParams, _list_: List[OilWellData], D: float) -> float:
    result: UniForceResult = UniForceResult()
    for item in _list_:
        N_d: float = result.N_sum
        f_res_all: float = all_liquid_resistance(params, _list_, item.depth)
        if (item.depth > 0) and (item.depth <= params.lc1):
            friction_f: float = params.mu1 * N_d * f_res_all
            print(friction_f)
            inc_dict = {}
            inc_dict[item.depth] = item.inclination
            diff_value = inc_dict.get(item.depth) - inc_dict.get(item.depth-params.L1)
            # []
            # 轴向合力
            resistance_f: float = params.w1 * params.L1 * math.cos(item.inclination - diff_value) - friction_f
            print(resistance_f)
            if resistance_f > 0:
                print(1)
                return 1
            elif resistance_f <= 0:
                print(2)
                return 2


# if __name__ == '__main__':
#     base_params: BasePassParams = get_test_params()
#     # 参数预处理
#     params: PassParams = params_init(base_params)
#     _list_ = get_test_data()
#     result1 = geometric_passability(params, _list_)
#     result2 = liquid_resistance(params, _list_, 15)
#     result3 = all_liquid_resistance(params, _list_, 15)
#     result4 = frictional_and_axial(params, _list_, 15)

